//
//  ViewController.m
//  TSS-Signing
//
//  Created by cole cabral on 2018-06-13.
//  Copyright © 2018 cole cabral. All rights reserved.
//

#import "ViewController.h"
#import "devices.h"
#import "LCLoadingHUD.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *firmwareArray;
    NSMutableArray *nameArray;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)fetchFirmware {
    //调用方法
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //调用结束
    [LCLoadingHUD showLoading:(@"Please wait...", @"Please wait...") inView:self.view];
    [NSTimer scheduledTimerWithTimeInterval:3.0f/*设置加载显示时长*/ target:self selector:@selector(dismissInViewHUD) userInfo:nil repeats:NO];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError *error;
        //加载一个NSURL对象
       NSURLRequest *BaseClass = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.ipsw.me/v4/device/%@?type=ipsw",deviceModel]]];
            NSData *response = [NSURLConnection sendSynchronousRequest:BaseClass returningResponse:nil error:nil];
            if (response) {
                NSDictionary *BaseClass = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
                NSDictionary *firmwares = [BaseClass objectForKey:@"firmwares"];
                self->firmwareArray = [NSMutableArray array];
                self->nameArray = [NSMutableArray array];
                for (id firm in firmwares) {
                    BOOL signedStatus = [[firm objectForKey:@"signed"] boolValue];
                    [self->firmwareArray addObject:firm];
                    [self.tableView reloadData];
                }
                NSLog(@"weatherInfo字典里面的内容为--》%@", firmwares );
            }
        //将请求的url数据放到NSData对象中
    });
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self fetchFirmware];
}

-(void)backgroundUpdate:(void (^)(UIBackgroundFetchResult))completionHandler {
    [self fetchFirmware];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -

- (void)dismissKeyHUD {
    [LCLoadingHUD hideInKeyWindow];
}

- (void)dismissInViewHUD {
    [LCLoadingHUD hideInView:self.view];
}

#pragma mark -
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [firmwareArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"tss";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    NSDictionary *firmware = [firmwareArray objectAtIndex:indexPath.row];
    BOOL signedStatus = [[firmware objectForKey:@"signed"] boolValue];
    if (signedStatus != 0){
        cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)", [firmware objectForKey:@"version"], [firmware objectForKey:@"buildid"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Model:%@   ✅固件正在签名，你可使用iTunes来恢复你的Apple设备", [firmware objectForKey:@"identifier"]];
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@(%@)", [firmware objectForKey:@"version"], [firmware objectForKey:@"buildid"], [firmware objectForKey:@"identifier"]];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"Model:%@  ❌固件未签名", [firmware objectForKey:@"identifier"]];
    }
    return cell;
}
- (IBAction)RE:(id)sender {
    //调用方法
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    //调用结束
    [LCLoadingHUD showLoading:(@"Please wait...", @"Please wait...") inView:self.view];
    [NSTimer scheduledTimerWithTimeInterval:3.0f/*设置加载显示时长*/ target:self selector:@selector(dismissInViewHUD) userInfo:nil repeats:NO];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSError *error;
        //加载一个NSURL对象
        NSURLRequest *BaseClass = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.ipsw.me/v4/device/%@?type=ipsw",deviceModel]]];
        NSData *response = [NSURLConnection sendSynchronousRequest:BaseClass returningResponse:nil error:nil];
        if (response) {
            NSDictionary *BaseClass = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
            NSDictionary *firmwares = [BaseClass objectForKey:@"firmwares"];
            self->firmwareArray = [NSMutableArray array];
            self->nameArray = [NSMutableArray array];
            for (id firm in firmwares) {
                BOOL signedStatus = [[firm objectForKey:@"signed"] boolValue];
                [self->firmwareArray addObject:firm];
                [self.tableView reloadData];
            }
            NSLog(@"weatherInfo字典里面的内容为--》%@", firmwares );
        }
        //将请求的url数据放到NSData对象中
    });
}
@end
